package com.sita.tboard.hitchhike.running;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.persistence.CcDriverTicketEntityDao;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.hitchhike.bean.TicketStatus;
import com.sita.tboard.hitchhike.detail.DriverTripDetailActivity;
import com.sita.tboard.util.DateUtils;
import com.sita.tboard.view.CursorRecyclerAdapter;

public class DriverRunningTripsAdapter extends CursorRecyclerAdapter<DriverRunningTripsAdapter.ViewHolder> {

    private Context mContext;

    public DriverRunningTripsAdapter(Context context, Cursor cursor) {
        super(cursor);
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hitchhike_driver_running, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolderCursor(ViewHolder holder, Cursor cursor) {
        long id = cursor.getLong(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.Id.columnName));
        String passengerId = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.PassengerId.columnName));
        String driverId = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.DriverId.columnName));
        long departureTime = cursor.getLong(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.DepartureTime.columnName));
        String fromLocation = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.FromLocation.columnName));
        String toLocation = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.ToLocation.columnName));
        String fromAddress = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.FromAddress.columnName));
        String toAddress = cursor.getString(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.ToAddress.columnName));
        float money = cursor.getFloat(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.Money.columnName));
        int status = cursor.getInt(cursor.getColumnIndex(CcDriverTicketEntityDao.Properties.Status.columnName));

        TicketModel ticket = new TicketModel();
        ticket.id = id;
        ticket.customId = Long.valueOf(passengerId);
        ticket.driverId = Long.valueOf(driverId);
        ticket.orderDepartureTime = departureTime;
        ticket.fromaddress = fromAddress;
        ticket.toaddress = toAddress;
        ticket.money = Double.valueOf(money);
        String[] startPosition = TextUtils.split(fromLocation, ",");
        ticket.startLng = Double.valueOf(startPosition[0]);
        ticket.startLat = Double.valueOf(startPosition[1]);
        String[] endPosition = TextUtils.split(toLocation, ",");
        ticket.endLng = Double.valueOf(endPosition[0]);
        ticket.endLat = Double.valueOf(endPosition[1]);
        ticket.status = status;

        holder.setItem(ticket);
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TicketModel mItem;

        private TextView mTimeView;
        private TextView mFromView;
        private TextView mToView;
        private TextView mStatusView;
        private View mView;

        public ViewHolder(View view) {
            super(view);
            mView = view;

            mTimeView = (TextView) view.findViewById(R.id.time);
            mFromView = (TextView) view.findViewById(R.id.from);
            mToView = (TextView) view.findViewById(R.id.to);
            mStatusView = (TextView) view.findViewById(R.id.status);
            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(DriverTripDetailActivity.newIntent(mItem));
                }
            });
        }

        public void setItem(TicketModel item) {
            mItem = item;
            mTimeView.setText(GlobalContext.getGlobalContext().getString(R.string.departure_time, DateUtils.formatDateTime(mItem.orderDepartureTime)));
            mFromView.setText(GlobalContext.getGlobalContext().getString(R.string.from, mItem.fromaddress));
            mToView.setText(GlobalContext.getGlobalContext().getString(R.string.to, mItem.toaddress));
            String status = TicketStatus.getInstance().getStatus(mItem.status);
            mStatusView.setText(status);
            if (mItem.status == TicketStatus.getInstance().UNPAID){
                mView.setClickable(false);
            } else {
                mView.setClickable(true);
            }
        }
    }

}
